// 联合单位 复选框
import {
  getQHJDList
} from '../../../api/check'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    value: {
      type: Number,
      optionalTypes: [String, Object],
      observer: function (newVal, oldVal) {
        // 属性值变化时执行-回显数据
        let currQh = {};
        let currQhIndex = 0;
        this.data.options[0].values.forEach((item, index) => {
          if (item.bm == newVal) {
            currQh = item
            currQhIndex = index
          }
        })
        if (currQh.bm) {
          this.setData({
            selectedDataQu: currQh,
            'options[1].values': currQh.jd,
            'options[0].defaultIndex': currQhIndex,
          }, function () {
            this.selectComponent('#address').setColumnIndex(0, currQhIndex)
          })
          let currJd = {};
          let currJdIndex = 0;
          this.data.options[1].values.forEach((item, index) => {
            if (item.bm == this.data.jd) {
              currJd = item
              currJdIndex = index
            }
          })
          if (currJd.bm) {
            this.setData({
              selectedDataJd: currJd,
              'options[1].defaultIndex': currJdIndex,
            }, function () {
              this.selectComponent('#address').setColumnIndex(1, currJdIndex)
            })
          } else {
            // 这里 企业的区划和街道对不上 所以要把之前的form数据清除掉
            this.setData({
              selectedDataJd: {}
            })
            this.triggerEvent('change', {
              qh: {
                bm: this.data.selectedDataQu.bm
              },
              jd: {
                bm: ''
              }
            })
          }
        } else {
          this.setData({
            selectedDataQu: {}
          })
        }
      }
    },
    jd: {
      type: String,
      value:'',
      optionalTypes: [String, Object]
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    selectedDataQu: {}, //当前选中区的值
    selectedDataJd: {}, //当前选中街道的值
    options: [{
      values: [],
      defaultIndex: 0,
    }, {
      values: [],
      defaultIndex: 0,
    }],
    selected: [] //选中的值
  },
  created() {
    this.getQuData()
  },
  ready() {},
  detached() {
    this.setData({
      selectedDataQu: {},
      selectedDataJd: {}, //当前选中街道的值
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 获取所属区的枚举值
    getQuData() {
      this.selectComponent('#quAndJd')
      const options = wx.getStorageSync('address')
      if (options && options.length > 0) {
        this.setData({
          'options[0].values': options,
          'options[1].values': options[0].jd
        })
      } else {
        getQHJDList().then(res => {
          let addressArrs = []
          res.forEach(item => {
            if (item.lx == "qhbm") {
              item.jd = res.filter(item1 => item1.pid == item.bm)
              addressArrs.push(item)
            }
          })
          this.setData({
            'options[0].values': addressArrs,
            'options[1].values': addressArrs[0].jd
          })
          wx.setStorageSync('address', addressArrs)
        })
      }
    },
    onChange(event) {
      const {
        index,
        value
      } = event.detail;
      let selected = []
      if (index == 0) {
        selected = [value[0], value[0].jd[0]]
      } else {
        selected = value
      }
      this.setData({
        'options[1].values': value[0].jd,
        selected: selected
      })
    },
    // 打开选择器
    open() {
      this.setData({
        show: true
      })
    },
    // 选择器确认事件
    confirm(event) {
      this.setData({
        selectedDataQu: this.data.selected[0],
        selectedDataJd: this.data.selected[1]
      })
      this.triggerEvent('change', {
        qh: {
          bm: this.data.selected[0].bm
        },
        jd: {
          bm: this.data.selected[1].bm
        }
      })
      this.colse()
    },
    // 关闭选择器
    colse() {
      this.setData({
        show: false,
      })
    }
  }
})